import java.lang.Math;
import java.util.Random;
// Estimating PI using the Monte Carlo method
/**
 *
 * @author mweya
 */
public class MonteCarlo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Initializing variables
        double area = 0;
        int times_in_circle = 0;
        // How many dots to make
        int times_to_run = 10000;
	// The radius of the circle
        double cradius = 0.5;

        int j = 0;
        while (j<times_to_run) {
            double[] point = makePoint(getRandom(), getRandom());
            if (inCircle(point, cradius)) {
                times_in_circle = times_in_circle+1;
            } 
            j = j+1;
        }

        area = times_in_circle*1.0/times_to_run*1.0
        double pi = area/(cradius*cradius);
        System.out.print("Pi = ");
        System.out.print(pi);
    }

    public static boolean inCircle(double[] point, double cradius) {
        double distance = Math.sqrt((point[0]-cradius)*(point[0]-cradius)+(point[1]-cradius)*(point[1]-cradius));
        if (distance<cradius){
            return true;
        } return false;
    }
    
    
    // works
    public static double[] makePoint(double x, double y) {
        double[] point = new double[2];
        point[0] = x;
        point[1] = y;
        return point;
    }
   
    public static double getRandom() {
        Random rand = new Random();
        double n = rand.nextDouble();
        return n;
    }
}